﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace HuseData.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        public string tabTitle = "";
        public string title = "";
        public string data = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["datetype"] != null)
                {
                    if (string.Compare(Request["datetype"], "Month", true) == 0)
                    {
                        binddataMonth();
                    }
                    else if (string.Compare(Request["datetype"], "week", true) == 0)
                    {
                        binddataWeek();
                    }
                    else
                    {
                        binddataday();
                    }
                }
                else
                {
                    binddataday();
                }
            }
        }

        void binddataday()
        {
            StringBuilder sb = new StringBuilder();

            DataAccess da = new DataAccess();

            List<string> titleDays = new List<string>();

            foreach (var item in da.GetHouseDayAll())
            {
                sb.AppendFormat("'{0}',",item.ToString("yyyy-MM-dd"));
                titleDays.Add(item.ToString("yyyy-MM-dd"));
            }
            if (sb.Length > 0)
            {
                title = sb.ToString();
                if (title.Length > 0)
                {
                    title = title.Remove(title.Length - 1);
                }
            }

            StringBuilder sbdata = new StringBuilder();

            adddata(sbdata, 5, 0, da,titleDays,ReportDateType.Day);
            adddata(sbdata, 5, 1, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 5, 2, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 2, 0, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 2, 1, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 2, 2, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 3, 0, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 3, 1, da, titleDays, ReportDateType.Day);
            adddata(sbdata, 3, 2, da, titleDays, ReportDateType.Day);           

            data= sbdata.ToString();

            StringBuilder sbtab = new StringBuilder();
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}'", areatxt(2), dataTypeTxt(3));
            
            tabTitle = sbtab.ToString();
        }


        void binddataMonth()
        {
            StringBuilder sb = new StringBuilder();

            DataAccess da = new DataAccess();

            List<string> titleDays = new List<string>();

            foreach (var item in da.GetHouseMonthAll())
            {
                sb.AppendFormat("'{0}',", item);
                titleDays.Add(item);
            }
            if (sb.Length > 0)
            {
                title = sb.ToString();
                if (title.Length > 0)
                {
                    title = title.Remove(title.Length - 1);
                }
            }

            StringBuilder sbdata = new StringBuilder();

            adddata(sbdata, 5, 0, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 5, 1, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 5, 2, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 2, 0, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 2, 1, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 2, 2, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 3, 0, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 3, 1, da, titleDays, ReportDateType.Month);
            adddata(sbdata, 3, 2, da, titleDays, ReportDateType.Month);

            data = sbdata.ToString();

            StringBuilder sbtab = new StringBuilder();
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}'", areatxt(2), dataTypeTxt(3));

            tabTitle = sbtab.ToString();
        }

        void binddataWeek()
        {
            StringBuilder sb = new StringBuilder();

            DataAccess da = new DataAccess();

            List<string> titleDays = new List<string>();

            foreach (var item in da.GetHouseWeekAll())
            {
                sb.AppendFormat("'{0}',", item);
                titleDays.Add(item);
            }
            if (sb.Length > 0)
            {
                title = sb.ToString();
                if (title.Length > 0)
                {
                    title = title.Remove(title.Length - 1);
                }
            }

            StringBuilder sbdata = new StringBuilder();

            adddata(sbdata, 5, 0, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 5, 1, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 5, 2, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 2, 0, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 2, 1, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 2, 2, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 3, 0, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 3, 1, da, titleDays, ReportDateType.Week);
            adddata(sbdata, 3, 2, da, titleDays, ReportDateType.Week);

            data = sbdata.ToString();

            StringBuilder sbtab = new StringBuilder();
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(5));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(2), dataTypeTxt(2));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(0), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}',", areatxt(1), dataTypeTxt(3));
            sbtab.AppendFormat("'{0}-{1}'", areatxt(2), dataTypeTxt(3));

            tabTitle = sbtab.ToString();
        }

        void adddata(StringBuilder sbdata, int datatype, int area,DataAccess da,List<string>titleDays, ReportDateType datetype)
        {
            sbdata.AppendFormat(@"{2}
                        name: '{0}-{1}',
                        type: 'line',
                        data: [", areatxt(area), dataTypeTxt(datatype),"{");

            Dictionary<string, HouseDataEntity> lst = da.GetHouseData(datatype, area, datetype); 

            foreach (var item in titleDays)
            {
                if (lst.ContainsKey(item))
                {
                    sbdata.AppendFormat("{0},", lst[item].Num);
                }
                else
                {
                    sbdata.AppendFormat("{0},",0);
                }
            }

            //foreach (var item in da.GetHouseData(datatype, area))
            //{
            //    sbdata.AppendFormat("{0},", item.Num);
            //}


            sbdata.Remove(sbdata.Length - 1, 1);

            sbdata.Append(@"]
                    },");
        }

        string areatxt(int a)
        {
            switch (a)
            { 
                case 0:
                    return "内环";
                case 1:
                    return "内外环";
                case 2:
                    return "外环";
                default:
                    return "未知:"+a;
            }
        }

        string dataTypeTxt(int a)
        {
            switch (a)
            {
                case 5:
                    return "均价";
                case 2:
                    return "售套";
                case 3:
                    return "售面";
                default:
                    return "未知类型:"+a;
            }
        }
    }
}